package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketAdminConfigService;
import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MarketAdminConfigServiceImpl implements MarketAdminConfigService {

    @Override
    public Map<String, String> getInitialExpress() {
        //创建一个返回结果值用来装载返回值
        HashMap<String, String> result = new HashMap<>();
        SqlSession session = MyBatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<Integer> list = new ArrayList<>();
        list.add(6);
        list.add(8);
        criteria.andIdIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            if (marketSystem != null) {
                result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
            }
        }
        return result;
    }

    @Override
    public void setExpress(String keyNameMin, String keyNameValue, String minMoney, String value) {
        SqlSession session = MyBatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample marketSystemExample1 = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        MarketSystemExample.Criteria criteria1 = marketSystemExample1.createCriteria();


        criteria.andKeyNameEqualTo(keyNameMin);
        MarketSystem marketSystem1 = mapper.selectOneByExample(marketSystemExample);
        marketSystem1.setKeyValue(minMoney);
        marketSystem1.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketSystem1);

        criteria1.andKeyNameEqualTo(keyNameValue);
        MarketSystem marketSystem2 = mapper.selectOneByExample(marketSystemExample1);
        marketSystem2.setKeyValue(value);
        marketSystem2.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketSystem2);

        session.commit();
        session.close();

    }

    @Override
    public Map<String, String> getInitialOrder() {
        //创建一个返回结果值用来装载返回值
        HashMap<String, String> result = new HashMap<>();
        SqlSession session = MyBatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<Integer> list = new ArrayList<>();
        list.add(11);
        list.add(4);
        list.add(1);
        criteria.andIdIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            if (marketSystem != null) {
                result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
            }
        }
        return result;
    }

    @Override
    public void setOrder(String comment, String unConfirm, String unpaid, String keyNameComment, String keyNameUnConfirm, String keyNameUnpaid) {
        SqlSession session = MyBatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample marketSystemExample1 = new MarketSystemExample();
        MarketSystemExample marketSystemExample2 = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        MarketSystemExample.Criteria criteria1 = marketSystemExample1.createCriteria();
        MarketSystemExample.Criteria criteria2 = marketSystemExample2.createCriteria();

        criteria.andKeyNameEqualTo(keyNameComment);
        MarketSystem marketSystem1 = mapper.selectOneByExample(marketSystemExample);
        marketSystem1.setKeyValue(comment);
        marketSystem1.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketSystem1);

        criteria1.andKeyNameEqualTo(keyNameUnConfirm);
        MarketSystem marketSystem2 = mapper.selectOneByExample(marketSystemExample1);
        marketSystem2.setKeyValue(unConfirm);
        marketSystem2.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketSystem2);

        criteria2.andKeyNameEqualTo(keyNameUnpaid);
        MarketSystem marketSystem3 = mapper.selectOneByExample(marketSystemExample2);
        marketSystem3.setKeyValue(unpaid);
        marketSystem3.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketSystem3);

        session.commit();
        session.close();
    }

    @Override
    public Map<String, String> getInitialWx() {
        //创建一个返回结果值用来装载返回值
        HashMap<String, String> result = new HashMap<>();
        SqlSession session = MyBatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(15);
        list.add(17);
        list.add(10);
        list.add(2);
        list.add(18);
        list.add(5);
        criteria.andIdIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            if (marketSystem != null) {
                result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
            }
        }
        return result;
    }

    @Override
    public void setWx(Map<String, String> loadValue) {

        SqlSession session = MyBatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);

        for (Map.Entry<String, String> stringStringEntry : loadValue.entrySet()) {
            if (stringStringEntry != null) {
                MarketSystemExample marketSystemExample = new MarketSystemExample();
                MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
                criteria.andKeyNameEqualTo(stringStringEntry.getKey());
                MarketSystem marketSystem = mapper.selectOneByExample(marketSystemExample);
                marketSystem.setKeyValue(stringStringEntry.getValue());
                marketSystem.setUpdateTime(LocalDateTime.now());
                mapper.updateByPrimaryKey(marketSystem);
            }
        }

        session.commit();
        session.close();
    }
}




